package cool.webstudy.admin.service.impl.callback;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import cool.webstudy.admin.constant.enums.CompetitionStatusEnum;
import cool.webstudy.admin.constant.enums.rescode.CompetitionResCodeEnum;
import cool.webstudy.admin.exception.BusinessException;
import cool.webstudy.admin.mapper.competition.CompetitionMapper;
import cool.webstudy.admin.model.dto.user.UserDetailInfoDTO;
import cool.webstudy.admin.model.po.competition.CompetitionPO;
import cool.webstudy.admin.service.callback.CallBack;

import java.util.Date;

/**
 * @author 莫振双
 * @date 2025/2/22 16:26
 * @description: 赛事修改状态回调方法
 */
public class CompetitionChangeStatusCallBack implements CallBack {
    // 赛事Dao层
    private CompetitionMapper competitionMapper;
    // 赛事业务编码
    private String unCode;
    // 当前你登录用户信息
    private UserDetailInfoDTO userDetailInfoDTO;

    public CompetitionChangeStatusCallBack() {
    }

    public CompetitionChangeStatusCallBack(CompetitionMapper competitionMapper, String unCode, UserDetailInfoDTO userDetailInfoDTO) {
        this.competitionMapper = competitionMapper;
        this.unCode = unCode;
        this.userDetailInfoDTO = userDetailInfoDTO;
    }

    @Override
    public Object run(Object... params) {
        if (params == null || params.length != 1) {
            throw new BusinessException(CompetitionResCodeEnum.RC_ERROR_UPDATE_STATUS);
        }
        if (params[0] instanceof CompetitionStatusEnum) {
            try {
                LambdaUpdateWrapper<CompetitionPO> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(CompetitionPO::getUnCode, this.unCode)
                        .set(CompetitionPO::getStatusFlag, params[0])
                        .set(CompetitionPO::getUpdateBy, userDetailInfoDTO.getAccount())
                        .set(CompetitionPO::getUpdateTime, new Date());
                competitionMapper.update(null, updateWrapper);
                return true;
            } catch (Exception e) {
                throw new BusinessException(CompetitionResCodeEnum.RC_ERROR_UPDATE_STATUS);
            }
        }
        throw new BusinessException(CompetitionResCodeEnum.RC_ERROR_UPDATE_STATUS);
    }
}
